package P35;

import org.junit.Test;

/**
 * @Author DJ同学
 * @Date 2021/2/27 10:13
 * @Version 1.0
 * @Name 搜索插入位置
 * @Problem https://leetcode-cn.com/problems/search-insert-position/
 * @Idea 基本的二分查找
 */
public class Solution {
    @Test
    public void test(){

        int i = searchInsert(new int[]{1, 3, 5, 6}, 5);
    }

    public int searchInsert(int[] nums, int target) {

        int left = 0;
        int right = nums.length-1;
        int mid;
        while(left<=right){
            mid = (left+right)>>1;//left + ((right - left) >> 1)
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]>target){//找了一个更小的数去大于target
                right = mid-1;
            }else{//找了一个更大的数去小于target
                left = mid+1;
            }
        }
        return left;
    }
}
